package org.dev.`as`.project.common.flutter

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import io.flutter.embedding.android.FlutterTextureView
import io.flutter.embedding.android.FlutterView
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.dart.DartExecutor
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import kotlinx.android.synthetic.main.fragment_flutter.*
import org.dev.`as`.project.common.R
import org.dev.`as`.project.common.ui.component.HiBaseFragment
import org.dev.hi.library.util.AppGlobals

/**
 * Author WenPing
 * CreateTime 2021/10/17.
 * Description:
 */
abstract class HiFlutterFragment() : HiBaseFragment() {

    protected var flutterEngine: FlutterEngine? = null

    protected var flutterView: FlutterView? = null

    // 在派生类中实现
    abstract val moduleName: String?

    init {
        flutterEngine = HiFlutterCacheManager.instance!!.getCachedFlutterEngine(
            moduleName!!, AppGlobals.get()!!
        )
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)

    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_flutter
    }

    fun setTitle(titleStr: String) {
        title.text = titleStr
        // 向flutter发送刷新
        title.setOnClickListener{
            HiFlutterBridge.instance!!.fire("onRefresh","so easy",object :MethodChannel.Result{
                override fun notImplemented() {

                }
                override fun error(errorCode: String?, errorMessage: String?, errorDetail: Any?) {
                    showToast(errorMessage)
                }
                override fun success(result: Any?) {
                    showToast(result as String)
                }
            })
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (layoutView as ViewGroup).addView(createFlutterView(activity!!))
    }

    fun createFlutterView(context: Context): FlutterView {
        // 使用FlutterTextureView 避免回退压后台
        val flutterTextureView = FlutterTextureView(activity!!)
        flutterView = FlutterView(context, flutterTextureView)
        return flutterView!!
    }

    override fun onStart() {
        flutterView!!.attachToFlutterEngine(flutterEngine!!)
        super.onStart()
    }

    override fun onResume() {
        super.onResume()
        //for flutter >= v1.17
        flutterEngine!!.lifecycleChannel.appIsResumed()
    }

    override fun onPause() {
        super.onPause()
        flutterEngine!!.lifecycleChannel.appIsInactive()
    }

    override fun onStop() {
        super.onStop()
        flutterEngine!!.lifecycleChannel.appIsPaused()
    }

    override fun onDetach() {
        super.onDetach()
        flutterEngine!!.lifecycleChannel.appIsDetached()
    }

    override fun onDestroy() {
        super.onDestroy()
        flutterView?.detachFromFlutterEngine()
    }
}